package com.study.easyexcel.common.domain;

import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.handler.CellWriteHandler;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 组合模板
 *
 * @author yuhuan
 * @date 2023/7/4
 */
public class GroupTemplate<T> extends TemplateSheet {

    private List<ListBodyStyleMap<T>> listMap;

    public void setListMap(List<ListBodyStyleMap<T>> listMap) {
        this.listMap = listMap;
    }

    public void addListMap(ListBodyStyleMap<T> listMap) {
        if (this.listMap == null) {
            this.listMap = ListUtils.newArrayList();
        }
        this.listMap.add(listMap);
    }

    public List<ListBodyStyleMap<T>> getListMap() {
        return listMap;
    }

    public List<CellWriteHandler> getAllWriteHandler() {
        if (listMap == null || listMap.size() == 0) {
            return ListUtils.newArrayList();
        }
        return this.listMap.stream().map(ListBodyStyleMap::getHandlers).filter(Objects::nonNull).collect(Collectors.toList());
    }

    public GroupTemplate<T> bodySorted() {
        listMap.sort(Comparator.comparing(ListBodyStyleMap::getSorted));
        return this;
    }

    public GroupTemplate<T> bodyReverse() {
        Collections.reverse(listMap);
        return this;
    }
}
